<?php

require dirname(__FILE__).'/include/common.inc.php';
$webglobal['file_brand'] = array();

$getDataFiles = array(
	"{$webconfig['dbpre']}brand_info.name","{$webconfig['dbpre']}brand_info.description",
	"{$webconfig['dbpre']}brand_info.status","{$webconfig['dbpre']}brand_info.logo",
	"{$webconfig['dbpre']}brand_info.area_id","{$webconfig['dbpre']}brand_info.description",
	"{$webconfig['dbpre']}brand_info.description",
	"{$webconfig['dbpre']}news.news_category_id","{$webconfig['dbpre']}news.from",
	"{$webconfig['dbpre']}news.title","{$webconfig['dbpre']}news.content",
	"{$webconfig['dbpre']}news.showtime","{$webconfig['dbpre']}news.weight",
	"{$webconfig['dbpre']}news.ishot","{$webconfig['dbpre']}news.image",
	"{$webconfig['dbpre']}news.shorttitle","{$webconfig['dbpre']}news.flag",
	"{$webconfig['dbpre']}news.writer","{$webconfig['dbpre']}news.channel",
	"{$webconfig['dbpre']}news.color","{$webconfig['dbpre']}news.keywords",
	"{$webconfig['dbpre']}news.description"
);

if ($webglobal['action'] == 'view') {
	define('PAGESCRIPT', 'vip_view');
	$newsCategoryDataModel = CategoryDataModel::factory('news', array('cache' => true));

	$webrequest['id'] = isset($webrequest['id']) ? intval($webrequest['id']) : 0;
	if ($webrequest['id']<=0) {
		global_ShowMessage("参数错误。");
	}

	$webglobal['file_brand']['brand'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}brand_info WHERE id = {$webrequest['id']} AND status = 1");
	if (!$webglobal['file_brand']['brand']) {
		global_ShowMessage("系统中没有该品牌。");
	}
	if ($webglobal['file_brand']['brand']['logo']) {
		$webglobal['file_brand']['brand']['thumbnail'] = array(
			'big' => getThumbnails($webglobal['file_brand']['brand']['logo'], 'big'),
			'small' => getThumbnails($webglobal['file_brand']['brand']['logo'], 'small')
		);
	}
	$webglobal['file_brand']['brand']['description'] = global_CutStr(SpHtml2Text($webglobal['file_brand']['brand']['description']), 200);

	//活动图集信息
	$imageList = array(
		'row'   => 3,
		'listtype'=> 'image.',
		'brand' => $webrequest['id'],
		'channel' => 2
	);
	$webglobal['file_brand']['image'] = getArcList($imageList);

	$imagelist = array(
		'titlelen' => '60',
		'row'   => 5,
		'brand' => $webrequest['id'],
		'channel' => 2
	);

	$webglobal['file_brand']['imagelist'] = getArcList($imagelist);
	
	$articlelist = array(
		'titlelen' => '60',
		'row'   => 8,
		'brand' => $webrequest['id'],
		'channel' => 1
	);
	$webglobal['file_brand']['articlelist'] = getArcList($articlelist);

	$hot = array(
		'orderby' => 'click',
		'row' => 10,
		'typeid' => 5
	);
	$webglobal['file_brand']['hot'] = getArcList($hot);

} else {

	define('PAGESCRIPT', 'vip');

	$webrequest['page'] = isset($webrequest['page']) ? max(1, intval($webrequest['page'])) : 1;
	$webglobal['file_brand']['perpage'] = 10;

	$searchSql = ' WHERE `status` = 1';
	$searchSql .= $webglobal['curcityid'] > 0 ? ' AND area_id = ' . $webglobal['curcityid'] : '';
	$webglobal['file_brand']['datanum'] = $db->GetNum("SELECT COUNT(*) FROM {$webconfig['dbpre']}brand_info{$searchSql}");

	$webglobal['file_brand']['dataarray'] = array();
	if ($webglobal['file_brand']['datanum'] > 0) {
		$option = array(
			'page' => '@@'
		);

		$webglobal['page'] = global_Page($webglobal['file_brand']['datanum'], $webglobal['file_brand']['perpage'], $webrequest['page'], buildURLForModule('brand', $option));

		$tempstartnum = ($webrequest['page']-1)*$webglobal['file_brand']['perpage'];

		$query = $db->Query("SELECT * FROM {$webconfig['dbpre']}brand_info{$searchSql} ORDER BY id DESC LIMIT $tempstartnum, {$webglobal['file_brand']['perpage']}");

		while ($tempdata = $db->FetchArray($query)) {
			if ($tempdata['logo']) {
				$tempdata['thumbnail'] = array(
					'big' => getThumbnails($tempdata['logo'], 'big'),
					'small' => getThumbnails($tempdata['logo'], 'small')
				);
			}
			$webglobal['file_brand']['dataarray'][$tempdata['id']] = $tempdata;
		}
	}

	//$filter = " WHERE addtime >= {$yesterDayTime} AND addtime < {$todayTime}";
	$filter = "WHERE `tm_company_comment`.status = 1";
	if ($webglobal['curcityid'] > 0) {
		$filter .= ' AND `tm_company`.city = ' . $webglobal['curcityid'];
	}

	$goroupSql = "SELECT `tm_company_comment`.company_id, COUNT(`tm_company_comment`.company_id) as countNum FROM `tm_company_comment` LEFT JOIN `tm_company` ON `tm_company_comment`.`company_id` = `tm_company`.`id`{$filter} GROUP BY `tm_company_comment`.company_id ORDER BY countNum DESC LIMIT 0, 5";

	$webglobal['file_companyComment_group'] = array();
	$companyId = array();
	$groupdata = $db->Query($goroupSql);
	while ($tempdata = $db->FetchArray($groupdata)) {
		
		$companyId[] = $tempdata['company_id'];

		$tempdata['addtime'] =  !empty($tempdata['addtime']) ? global_FormatTime($tempdata['addtime']) : '';
		$webglobal['file_companyComment_group'][$tempdata['company_id']] = $tempdata;
	}

	//查询商家信息
	if ($companyId) {
		$companySql = "SELECT id, name, image FROM `tm_company` WHERE id IN (". implode(',', $companyId) .")";
		$query = $db->Query($companySql);
		while ($tempdata = $db->FetchArray($query)) {
			if (isset($webglobal['file_companyComment_group'][$tempdata['id']])) {
				$webglobal['file_companyComment_group'][$tempdata['id']]['name'] = $tempdata['name'];
				$webglobal['file_companyComment_group'][$tempdata['id']]['image'] = $tempdata['image'];
			}
		}
	}
	
}

template_Display('brand');// PAGE NOT ADDED